﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Timers;
using NutShell.DesignPattern;
using JDKD.BMCS.Instruction;
using System.Diagnostics;

namespace JDKD.BMCS.Business
{
        public class BusinessCache : INotifyPropertyChanged
        {
                public BusinessCache()
                {
                        Graphicses = new Dictionary<int, Graphics>(4);
                        CurrentStep = 1;

                        Load();

                        ShiftOperations = new List<ShiftOperation>();

                        MainTimer.Elapsed += MainTimer_Elapsed;
                        MainTimer.Enabled = true;

                        SerialPortCommunication.PushStateUpdated += new EventHandler<PushStateEventArgs>(SerialPortCommunication_PushStateUpdated);
                }

                

                #region 常量

                public const int CamerasCount = 4;

                #endregion 常量

                private Timer MainTimer = new Timer(500);

                #region 属性

                public Dictionary<Int32, Graphics> Graphicses { get; private set; }

                public Dictionary<Int32, GraphicsCard> GraphicsCards { get; private set; }

                public Dictionary<Int32, Group> Groups { get; private set; }

                public Dictionary<Int32, Camera> Cameras { get; private set; }

                public Dictionary<Int32, Channel> Channels { get; private set; }

                public List<Area> Aeras { get; private set; }

                public Camera CurrentCamera { get; set; }

                public Area CurrentArea { get; set; }

                public int CurrentStep { get; set; }

                public bool IsShowBackAera { get; set; }

                public List<ShiftOperation> ShiftOperations { get; set; }

                private bool isLogined = false;

                public bool IsLogined
                {
                        get
                        {
                                return isLogined;
                        }
                        set
                        {
                                isLogined = value;
                                OnPropertyChanged("IsLogined");
                                OnPropertyChanged("LoginText");
                        }
                }

                public String LoginText
                {
                        get
                        {
                                if (IsLogined)
                                {
                                        return "已登录";
                                }
                                return "未登录";
                        }
                }

                public DateTime Now { get; private set; }

                #endregion 属性

                #region 事件

                public event PropertyChangedEventHandler PropertyChanged;

                #endregion 事件

                #region 方法

                public void OnPropertyChanged(string propertyName)
                {
                        if (PropertyChanged != null)
                        {
                                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                        }
                }

                private void MainTimer_Elapsed(object sender, ElapsedEventArgs e)
                {
                        Now = DateTime.Now;
                        OnPropertyChanged("Now");

                        foreach (var item in Channels.Values)
                        {
                                if (item.ShiftingCounter > 0)
                                {
                                        item.ShiftingCounter++;
                                }
                        }
                }

                private void SerialPortCommunication_PushStateUpdated(object sender, PushStateEventArgs e)
                {
                        if (e != null)
                        {
                                Groups[1].IsPushing = e.IsLeftMotion;
                                Groups[2].IsPushing = e.IsRightMotion;

                        }
                }

                public bool Load()
                {
                        if (!Singleton<ModelCache>.Instance.Load())
                        {
                                return false;
                        }

                        Aeras = new List<Area>();
                        foreach (var item in Singleton<ModelCache>.Instance.AeraModels)
                        {
                                Aeras.Add(new Area(item));
                        }

                        Channels = new Dictionary<int, Channel>();
                        foreach (var item in Singleton<ModelCache>.Instance.ChannelModels)
                        {
                                Channels.Add(item.Id, new Channel(item));
                        }

                        Cameras = new Dictionary<int, Camera>();
                        for (int i = 1; i != 5; i++)
                        {
                                Cameras.Add(i, new Camera(i));
                        }

                        Groups = new Dictionary<int, Group>(2);
                        for (int i = 1; i < 3; i++)
                        {
                                Groups.Add(i, new Group(i));
                        }

                        GraphicsCards = new Dictionary<int, GraphicsCard>();
                        for (int i = 1; i != 5; i++)
                        {
                                GraphicsCards.Add(i, new GraphicsCard(i));
                        }

                        foreach (var item in GraphicsCards.Values)
                        {
                                item.StartGraphicsCard();
                                item.SetVideoSource(0);
                                item.StartSnapShotAsync();
                        }

                        return true;
                }

                public void Save()
                {
                        Singleton<ModelCache>.Instance.DataContext.SubmitChanges();
                }

                #endregion 方法
        }
}